#include <iostream>
#include <deque>
#include <vector>
using namespace std;

class Solution {
public:
    int minOperations(vector<int>& nums, int x) {
        long long sum = 0;
        for (auto e : nums) sum += e;
        long long target = sum - x;

        if (sum < x) return -1;
        if (sum == x) return nums.size();

        deque<int> dq;
        int len = 0, res = 0;
        for (int i = 0; i < nums.size(); ++i) {
            len += nums[i];
            dq.push_back(i);

            while (len >= target) {
                if (len == target) {
                    res = max(res, (int)dq.size());
                    break;
                }
                len -= nums[dq.front()];
                dq.pop_front();
            }
        }
        return res == 0 ? -1 : nums.size() - res;
    }
};

int main()
{
    vector<int> nums = { 8828,9581,49,9818,9974,9869,9991,10000,10000,10000,9999,9993,9904,8819,1231,6309 };
    Solution().minOperations(nums, 134365);

    return 0;
}